﻿package com.fox2d 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	
	/**
	 * ...
	 * @author Orlando Leite
	 */
	public class Object2D extends Sprite
	{
		protected var life:Number = Infinity;
		
		protected var game:Game2D;
		protected var walkMap:BitmapData;
		protected var referenceBitmap:Bitmap;
		protected var _reference:Sprite;
		
		public function Object2D() 
		{
			referenceBitmap = new Bitmap();
			
			var tempReference:Sprite = new Sprite();
			tempReference.graphics.beginFill( 0xFF0000 );
			tempReference.graphics.drawCircle( 6, 6, 6 );
			tempReference.graphics.endFill();
			
			reference = tempReference;
		}
		
		public function init( game:Game2D, walkMap:BitmapData ):void
		{
			if ( game != null )
			{
				this.game = game;
				this.walkMap = walkMap;
			}
			else
			{
				throw new Error( "this is already initied" )
			}
		}
		
		public function set reference( value:Sprite ):void
		{
			_reference = value;
			var temp:BitmapData = new BitmapData( _reference.width, _reference.height, true, 0x0 );
			temp.draw( _reference );
			referenceBitmap.bitmapData = temp;
		}
		
		public function get hitter():Bitmap
		{
			return referenceBitmap;
		}
		
		public function die():void
		{
			game.removeObject( this );
		}
		
		public function addDamage( value:int ):void
		{
			if ( life != Infinity ) life -= value;
			
			if ( life == 0 ) die();
		}
		
	}
	
}